package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.domain.Food;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.FoodService;
import com.itheima.util.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/*
    膳食表现层
 */
@RestController
@RequestMapping("/food")
public class FoodController {

    //注入FoodService
    @Reference
    private FoodService foodService;

    //上传文件
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile){
        try {
            //读取Excel文件
            List<String[]> list = POIUtils.readExcel(excelFile);
            //对list进行判断
            if (list != null && list.size() > 0) {
                //list不为空 将list转为List<Food> 格式
                List<Food> foodList = new ArrayList<>();
                for (String[] strings : list) {
                    //将list中的每一个元素进行转化
                    String name = strings[0];
                    String serialNumber = strings[1];
                    String consumption = strings[2];
                    String applicableAge = strings[3];
                    Food food = new Food(name,serialNumber,consumption,applicableAge);
                    foodList.add(food);
                }
                //调用添加方法
                foodService.add(foodList);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
        return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }


    //添加膳食项
    @RequestMapping("/addition")
    public Result add(@RequestBody Food food){
        try{
            //调用服务层接口
            foodService.addition(food);
            //添加成功,返回结果
            return new Result(true,MessageConstant.ADD_FOOD_SUCCESS);

        }catch (Exception e){
            e.printStackTrace();
            //添加失败,返回失败信息
            return new Result(false,MessageConstant.ADD_FOOD_FAIL);
        }
    }


    //分页查询所有膳食信息
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        //调用服务层接口
        PageResult pageResult =foodService.findPage(queryPageBean);
        //查询成功,返回结果
        return pageResult;
    }


    //编辑窗口回显
    @RequestMapping("/findById")
    public Result findById(int id){
        try {
            //调用服务层接口
            Food food = foodService.findById(id);
            //回显成功,返回结果
            return new Result(true, MessageConstant.QUERY_FOOD_SUCCESS,food);
        }catch (Exception e){
            e.printStackTrace();
            //回显失败,返回错误信息
            return new Result(false,MessageConstant.QUERY_FOOD_FAIL);
        }
    }

    //编辑膳食项
    @RequestMapping("/edit")
    public Result edit(@RequestBody Food food){
        try{
            //调用服务层接口
            foodService.edit(food);
            //编辑成功,返回结果
            return new Result(true,MessageConstant.EDIT_FOOD_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            //编辑失败,返回错误信息
            return new Result(false,MessageConstant.EDIT_FOOD_FAIL);
        }
    }

    //删除膳食项
    @RequestMapping("/delete")
    public Result delete(int id){
        try{
            //调用服务层接口
            foodService.delete(id);
            //删除成功,返回结果
            return new Result(true,MessageConstant.DELETE_FOOD_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            //删除失败,返回错误信息
            return new Result(false,MessageConstant.DELETE_FOOD_FAIL);
        }
    }


    //健康干预显示膳食信息
    @RequestMapping("/findAll")
    public Result findAllFood(){
        try {
            //调用服务层接口
            List<Food> list = foodService.findAllFood();
            //返回成功结果
            return new Result(true,MessageConstant.QUERY_FOOD_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            //查询失败,返回错误信息
            return new Result(false,MessageConstant.QUERY_FOOD_FAIL);
        }
    }
}
